package cn.dyp.com.querydata.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.dyp.com.querydata.R;
import cn.dyp.com.querydata.db.BaseDB;
import cn.dyp.com.querydata.db.SensorValueDB;
import cn.dyp.com.querydata.util.ChartPagerBean;
import cn.dyp.com.querydata.util.ChartView;
import cn.dyp.com.querydata.util.SensorValue;
import cn.dyp.com.querydata.util.WheelView;

/**
 * App的主类，控制程序的运作
 * 
 * @author asus
 *
 */
public class MainActivity extends AppCompatActivity {
	private TextView mShowTV;
	private SensorValueDB mDb;
	private ArrayList<Object> mList;
	private ChartView mChartView;
	private ChartPagerBean mBean;
	private List<ChartPagerBean> mBeans;
	private long[] times;
	private long currentTime = 1000 * 60;
	private String[] sensors;
	private String[] timers;
	private Random mRandom;
	private View mView;
	private WheelView mSensorView;
	private WheelView mTimerView;
	private int sensorindex = 0;
	private int timeindex = 0;
	private AlertDialog.Builder mBuilder;
	private Handler mHandler;
	private Runnable mRunnable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().hide();
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		initVziew();
		mDb = new SensorValueDB(this);
		mDb.deleteAllRows();
		mHandler = new Handler();
		mRunnable = new Runnable() {

			@Override
			public void run() {
				saveData();
				mHandler.postDelayed(mRunnable, 1000 * 20);
			}
		};
		mHandler.postDelayed(mRunnable, 500);
		mChartView = new ChartView();
		mRandom = new Random();
		iniData();
		mBean = mBeans.get(0);
	}

	/**
	 * 初始化视图
	 * 
	 */
	private void initVziew() {
		mShowTV = (TextView) findViewById(R.id.show_wheel);
		mShowTV.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showWheelView();
			}
		});
	}

	/**
	 * 初始化数据库
	 * 
	 */
	private void saveData() {
		mList.add(new SensorValue(System.currentTimeMillis(), mRandom
				.nextInt(80), mRandom.nextInt(4500), mRandom.nextInt(25),
				mRandom.nextInt(30), mRandom.nextInt(20), mRandom.nextInt(60)));
		mDb.saveDataToDB(mList, null);
	}

	/**
	 * 初始化数据
	 * 
	 */
	private void iniData() {
		times = new long[] { 1000 * 60, 1000 * 300, 1000 * 600 };
		// 创建滑轮显示的数组
		sensors = new String[] { getString(R.string.sensor_co2),
				getString(R.string.sensor_light),
				getString(R.string.sensor_air), getString(R.string.sensor_soil) };
		timers = new String[] { getString(R.string.time_1),
				getString(R.string.time_5), getString(R.string.time_10) };
		// 生成要保存的数据列表
		mList = new ArrayList<Object>();

		// 创建传感器容器
		mBeans = new ArrayList<ChartPagerBean>();
		mBean = new ChartPagerBean(getString(R.string.co2));
		mBean.majorMax = 100;
		mBean.majorMin = 0;
		mBean.majorWarningMax = 50;
		mBean.majorWarningMin = 10;
		mBeans.add(mBean);

		mBean = new ChartPagerBean(getString(R.string.light));
		mBean.majorMax = 6000;
		mBean.majorMin = 0;
		mBean.majorWarningMax = 4500;
		mBean.majorWarningMin = 1000;
		mBeans.add(mBean);

		mBean = new ChartPagerBean(getString(R.string.air_t));
		mBean.majorMax = 100;
		mBean.majorMin = 0;
		mBean.majorWarningMax = 40;
		mBean.majorWarningMin = 0;
		mBean.isHasSlave = true;
		mBean.slaveMax = 100;
		mBean.slaveMin = 0;
		mBean.slaveName = getString(R.string.air_h);
		mBean.slaveWarningMax = 50;
		mBean.slaveWarningMin = 10;
		mBeans.add(mBean);

		mBean = new ChartPagerBean(getString(R.string.soil_t));
		mBean.majorMax = 100;
		mBean.majorMin = 0;
		mBean.majorWarningMax = 45;
		mBean.majorWarningMin = 10;
		mBean.isHasSlave = true;
		mBean.slaveMax = 100;
		mBean.slaveMin = 15;
		mBean.slaveName = getString(R.string.soil_h);
		mBean.slaveWarningMax = 50;
		mBean.slaveWarningMin = 10;
		mBeans.add(mBean);
	}

	/**
	 * 显示选择项
	 * 
	 */
	private void showWheelView() {
		initWheelView();
		setWheelViewListener();
		initDialog();
		mBuilder.show();
	}

	/**
	 * 显示可选择传感器和时间的Dialog
	 * 
	 */
	private void initDialog() {
		mSensorView.setSeletion(sensorindex);
		mTimerView.setSeletion(timeindex);
		mBuilder = new AlertDialog.Builder(MainActivity.this);
		mBuilder.setTitle(getString(R.string.choose_titl)).setView(mView)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						mDb.loadSensorByTime(
								new Date().getTime() - currentTime,
								new BaseDB.OnDatabaseHandlerListener() {

									@Override
									public boolean onDatabaseHandler(
											ArrayList<Object> dataList,
											boolean isSuccess) {
										if (isSuccess) {
											for (int i = 0; i < mBeans.size(); i++) {
												mBeans.get(i).majorValueList
														.clear();
												if (mBeans.get(i).isHasSlave) {
													mBeans.get(i).slaveValueList
															.clear();
												}
											}
											for (int i = 0; i < dataList.size(); i++) {
												SensorValue value = (SensorValue) dataList
														.get(i);
												mBeans.get(0).majorValueList
														.add(value.getCo2());
												mBeans.get(1).majorValueList
														.add(value.getLight());
												mBeans.get(2).majorValueList.add(value
														.getAirTemper());
												mBeans.get(2).slaveValueList.add(value
														.getAirHumid() * 2);
												mBeans.get(3).majorValueList.add(value
														.getSoilTemper());
												mBeans.get(3).slaveValueList.add(value
														.getSoilHumid() * 2);
											}
										}
										mChartView
												.draw(MainActivity.this,
														mBean,
														(LinearLayout) findViewById(R.id.chart_show),
														8);
										return false;
									}
								});
					}
				});
	}

	/**
	 * 给滑动滚轴设置监听器
	 * 
	 */
	private void setWheelViewListener() {
		mSensorView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				sensorindex = selectedIndex - 1;
				mBean = mBeans.get(sensorindex);
			}
		});
		mTimerView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {

			@Override
			public void onSelected(int selectedIndex, String item) {
				super.onSelected(selectedIndex, item);
				timeindex = selectedIndex - 1;
				currentTime = times[timeindex];
			}
		});
	}

	/**
	 * 初始化滚轴视图
	 * 
	 */
	private void initWheelView() {
		mView = LayoutInflater.from(MainActivity.this).inflate(
				R.layout.wheel_view, null);
		mSensorView = (WheelView) mView.findViewById(R.id.sensor_view);
		mTimerView = (WheelView) mView.findViewById(R.id.time_view);
		mSensorView.setItems(Arrays.asList(sensors));
		mTimerView.setItems(Arrays.asList(timers));
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mDb != null) {
			mDb.close();
		}
	}
}
